package com.rtech.app.service;

import java.io.File;

import org.springframework.stereotype.Service;

import com.rtech.app.constant.TraversalConstant;
import com.rtech.model.LoactionInfo;

@Service
public class TraversalServiceImpl implements TraversalService {

	public com.rtech.model.File getFileInformation(String filePath) {
		com.rtech.model.File fileDetail = new com.rtech.model.File();
		java.io.File f = new java.io.File(filePath);
		String permission = "";
		if(f.exists()) {
			fileDetail.setExist(true);
			permission += f.canRead() ? TraversalConstant.PERMISSION_READ : "";
			permission += f.canWrite() ? TraversalConstant.PERMISSION_WRITE : "";
			permission += f.canExecute() ? TraversalConstant.PERMISSION_EXECUTE : "";
			fileDetail.setPermissions(permission);
		} else {
			fileDetail.setExist(false);
		}
		return fileDetail; 
	}
	
	


	public LoactionInfo getCompletePathInfo(String path) {

		java.io.File f = new java.io.File(path);
		traverse(f);
		
		return null;
		
//		XStream xs = new XStream();
//		xs.processAnnotations(Directory.class);
//		xs.processAnnotations(File.class);
//		xs.processAnnotations(LoactionInfo.class);
//		xs.alias("path", List.class);
//
//		List<Book> books = (List<Book>) xs.fromXML(fakeData);

	
	}
	
	/**
	 * 
	 * @param file
	 */
	private void traverse(File file) {
		System.out.println(file);
		
		if (file.isDirectory()) {
			
			String entries[] = file.list();
			if (entries != null) {
				for (String entry : entries) {
					traverse(new File(file, entry));
				}
			}
		} else {
			// put information in file and then append into Location Information.
		}
	}

}
